/*******************************************************************************
																				
	DESCRIPTION:  	This do file generates appendix Table A4.

*******************************************************************************/

clear all
global id_code 111_2
pause on
set seed 2110

* Set year:
local year 2006


/*******************************************************************************
*	Predicted job-finding rates - time series - predictions done by year
********************************************************************************/

* Create frame for results:
frame create sum str1000(vars) R_sq beta cov

local i = 1
foreach model in  ///
			/// Employment history models, variable groups: 
            BasicWithEmplHist_Basic BasicWithEmplHist_Yminus1  BasicWithEmplHist_Yminus2 ///
            BasicWithEmplHist_Yminus3 BasicWithEmplHist_Yminus4 BasicWithEmplHist_Yminus5 BasicWithEmplHist_All ///
            BasicWithEmplHist_Marg_Yminus1 BasicWithEmplHist_Marg_Yminus2 BasicWithEmplHist_Marg_Yminus3 ///
            BasicWithEmplHist_Marg_Yminus4 BasicWithEmplHist_Marg_Yminus5 BasicWithEmplHist_Marg_PreUnemp ///
            /// Individual variables;
            BasicWithEmplHist_IndivVars_Seq_DaysUnemp_2Years BasicWithEmplHist_IndivVars_Seq_unemplSpells2Ybefore ///
            BasicWithEmplHist_IndivVars_Seq_nEmployers2Y BasicWithEmplHist_IndivVars_Seq_DaysOnDI_2Years  BasicWithEmplHist_IndivVars_Seq_tenure ///
            BasicWithEmplHist_IndivVars_Seq_L_nEmployees_L1L2 BasicWithEmplHist_IndivVars_Seq_L_firmSizeChange_L1L2 ///
            BasicWithEmplHist_IndivVars_Seq_L_layoffRate_L1L2 BasicWithEmplHist_IndivVars_Seq_missings ///
            BasicWithEmplHist_IndivVars_Seq ///
            BasicWithEmplHist_IndivVars_Marg_DaysUnemp_2Years BasicWithEmplHist_IndivVars_Marg_unemplSpells2Ybefore /// 
            BasicWithEmplHist_IndivVars_Marg_nEmployers2Y BasicWithEmplHist_IndivVars_Marg_DaysOnDI_2Years  BasicWithEmplHist_IndivVars_Marg_tenure ///
            BasicWithEmplHist_IndivVars_Marg_L_nEmployees_L1L2 BasicWithEmplHist_IndivVars_Marg_L_firmSizeChange_L1L2 ///
            BasicWithEmplHist_IndivVars_Marg_L_layoffRate_L1L2 BasicWithEmplHist_IndivVars_Marg_missings {

	* Import data:
	use "${data}/003_MainWithEnsemblePred_`model'_`year'.dta", clear

	* Calculate the beta and the R-squared from regressing the empirical JFR on the predicted JFR
	reg emplAft6M_0M_In p_emplAft6M_0M_In
	
	local R_sq = e(r2)
	local beta = _b[p_emplAft6M_0M_In]
	
	* Calculate the covariance between the empirical JFR and the predicted JFR
	correlate emplAft6M_0M_In p_emplAft6M_0M_In, covariance
	local cov = r(cov_12)		
	
	 * Save to frame
	frame post sum ("`model'") (`R_sq')	(`beta') (`cov')		

}

* Split the results:
frame change sum
frame put vars R_sq in 1/13, into(groups) 
frame put vars R_sq in 14/32, into(indiv)

/*******************************************************************************
*	Long table
********************************************************************************/

* Generate table with variable groups:
frame change groups

* Fix variable names:
replace vars = "BasicWithEmplHist_PreUnemp" if vars == "BasicWithEmplHist_All"
replace vars = subinstr(vars, "Marg_", "", .)
replace vars = subinstr(vars, "BasicWithEmplHist_", "", .)

* Gen marginal / sequential indicator:
gen j = 1 in 1/7
replace j = 2 in 8/13
label define j 1 "Sequential" 2 "Marginal"
label values j j

* Transform to wide format:
reshape wide R_sq, i(vars) j(j)
sort R_sq1

* Add percentage increases:
gen incr1_temp = .

forval i = 2/7 {
	replace incr1_temp = 100 * (R_sq1 / R_sq1[`=`i'-1'] - 1) in `i'
}

gen incr2_temp = 100 * (R_sq2 / R_sq1[1] - 1)

* Format them properly:
gen incr1 = "+" + string(incr1_temp, "%9.1f") + "\%" if incr1_temp>0
replace incr1 = string(incr1_temp, "%9.1f") + "\%" if incr1_temp<=0
gen incr2 = "+" + string(incr2_temp, "%9.1f") + "\%" if incr2_temp>0
replace incr2 = string(incr2_temp, "%9.1f") + "\%" if incr2_temp<=0

drop incr1_temp incr2_temp

* Re-order variables:
order vars R_sq1 incr1 R_sq2 incr2


* Generate table with individual variables:
frame change indiv

* Fix variable names:
replace vars = "Basic" in 1
forval i = 2/10 {
	replace vars = vars[`=`i'+9'] in `i'
}
replace vars = subinstr(vars, "BasicWithEmplHist_IndivVars_Marg_", "", .)

* Gen marginal / sequential indicator:
gen j = 1 in 1/10
replace j = 2 in 11/19
label define j 1 "Sequential" 2 "Marginal"
label values j j

gen ord = _n in 1/10
replace ord = _n-9 in 11/19

* Transform to wide format:
reshape wide R_sq, i(vars) j(j)
sort ord
drop ord

* Add percentage increases:
gen incr1_temp = .

forval i = 2/10 {
	replace incr1_temp = 100 * (R_sq1 / R_sq1[`=`i'-1'] - 1) in `i'
}

gen incr2_temp = 100 * (R_sq2 / R_sq1[1] - 1)

* Format them properly:
gen incr1 = "+" + string(incr1_temp, "%9.1f") + "\%" if incr1_temp>0
replace incr1 = string(incr1_temp, "%9.1f") + "\%" if incr1_temp<=0
gen incr2 = "+" + string(incr2_temp, "%9.1f") + "\%" if incr2_temp>0
replace incr2 = string(incr2_temp, "%9.1f") + "\%" if incr2_temp<=0

drop incr1_temp incr2_temp

* Re-order variables:
order vars R_sq1 incr1 R_sq2 incr2

* Keep only first five rows:
keep in 1/5


* Combined table:
frame change groups

file open myfile using "${output}/${id_code}_Explanatory_power_of_variables_EmploymentHistory_Combined_table.tex", write replace

file write myfile "%\documentclass{article}" _newline ///
	"%\usepackage{booktabs}" _newline ///
	"%\usepackage[margin=1in]{geometry}" _newline ///
	"%\begin{document}" _newline ///
	"%\begin{table}[h] \centering" _newline ///
	"\footnotesize \begin{tabular}{l c c c c c c c}" _newline ///
	"\hline \hline \addlinespace[3ex]" _newline ///
	///
	"& \multicolumn{7}{c}{\large \textbf{A.  Groups of variables: sequential sub-models}} \\ \addlinespace[3ex]" _newline ///
	" & \normalsize (1) & \normalsize (2) & \normalsize (3) & \normalsize (4) & \normalsize (5) & \normalsize (6) & \normalsize (7) \\ \addlinespace[1.5ex]" _newline ///
	"\cline{2-8} \addlinespace[1.5ex]" _newline ///
	"\normalsize \(R^2(\hat{F}_{0}, F_{0})\) & \normalsize `:di %9.3f `= R_sq1[1]'' & \normalsize `:di %9.3f `=R_sq1[2]'' & \normalsize `:di %9.3f `=R_sq1[3]'' & \normalsize `:di %9.3f `=R_sq1[4]'' & \normalsize `:di %9.3f `=R_sq1[5]'' & \normalsize `:di %9.3f `=R_sq1[6]'' & \normalsize `:di %9.3f `=R_sq1[7]''  \\ \addlinespace[0.2cm]" _newline ///
	"Change (\(j\)) vs (\(j-1\)) & - & `= incr1[2]' & `= incr1[3]' & `= incr1[4]' & `= incr1[5]' & `= incr1[6]' & `= incr1[7]'  \\ \addlinespace[3ex]" _newline ///
	"\cline{2-8} \addlinespace[1.5ex]" _newline ///
	"\small Basic Socio-demographics & X & X & X & X & X & X & X \\ \addlinespace[0cm]" _newline ///
	"\small Individual History in \(t-1\) & & X & X & X & X & X & X \\ \addlinespace[0cm]" _newline ///
	"\small Individual History in \(t-2\) & & & X & X & X & X & X \\ \addlinespace[0cm]" _newline ///
	"\small Individual History in \(t-3\) & & & & X & X & X & X \\ \addlinespace[0cm]" _newline ///
	"\small Individual History in \(t-4\) & & & & & X & X & X \\ \addlinespace[0cm]" _newline ///
	"\small Individual History in \(t-5\) & & & & & & X & X \\ \addlinespace[0cm]" _newline ///
	"\small Firm Characteristics & & & & & & & X \\ \addlinespace[0.2cm]" _newline ///
	" \addlinespace[6ex]" _newline ///
	///
	"& \multicolumn{7}{c}{\large \textbf{B. Groups of variables: marginal sub-models}} \\ \addlinespace[3ex]" _newline ///
	" & \normalsize (1) & \normalsize (2) & \normalsize (3) & \normalsize (4) & \normalsize (5) & \normalsize (6) & \normalsize (7) \\ \addlinespace[1.5ex]" _newline ///
	"\cline{2-8} \addlinespace[1.5ex]" _newline ///
	"\normalsize \(R^2(\hat{F}_{0}, F_{0})\) & \normalsize `:di %9.3f `= R_sq1[1]'' & \normalsize `:di %9.3f `=R_sq2[2]'' & \normalsize `:di %9.3f `=R_sq2[3]'' & \normalsize `:di %9.3f `=R_sq2[4]'' & \normalsize `:di %9.3f `=R_sq2[5]'' & \normalsize `:di %9.3f `=R_sq2[6]'' & \normalsize `:di %9.3f `=R_sq2[7]'' \\ \addlinespace[0.2cm]" _newline ///
	"Change (\(j\)) vs (1) & - & `= incr2[2]' & `= incr2[3]' & `= incr2[4]' & `= incr2[5]' & `= incr2[6]' & `= incr2[7]' \\ \addlinespace[3ex]" _newline ///
	"\cline{2-8} \addlinespace[1.5ex]" _newline ///
	"\normalsize \textbf{Variables:} & & & & & & & \\ \addlinespace[1.5ex]" _newline ///	
	"\small Basic Socio-demographics & X & X & X & X & X & X & X \\ \addlinespace[0cm]" _newline ///
	"\small Individual History in \(t-1\) & & X & & & & &  \\ \addlinespace[0cm]" _newline ///
	"\small Individual History in \(t-2\) & & & X & & & & \\ \addlinespace[0cm]" _newline ///
	"\small Individual History in \(t-3\) & & & & X & & & \\ \addlinespace[0cm]" _newline ///
	"\small Individual History in \(t-4\) & & & & & X & & \\ \addlinespace[0cm]" _newline ///
	"\small Individual History in \(t-5\) & & & & & & X & \\ \addlinespace[0cm]" _newline ///
	"\small Firm Characteristics & & & & & & & X \\ \addlinespace[0cm]" _newline ///
	" \addlinespace[6ex]" _newline

frame change indiv
	
file write myfile "& \multicolumn{7}{c}{\large \textbf{C. Individual variables: sequential sub-models}} \\ \addlinespace[3ex]" _newline ///
	" & \normalsize (1) & \normalsize (2) & \normalsize (3) & \normalsize (4) & \normalsize (5) & & \\ \addlinespace[1.5ex]" _newline ///
	"\cline{2-8} \addlinespace[1.5ex]" _newline ///
	"\normalsize \(R^2(\hat{F}_{0}, F_{0})\) & \normalsize `:di %9.3f `= R_sq1[1]'' & \normalsize `:di %9.3f `=R_sq1[2]'' & \normalsize `:di %9.3f `=R_sq1[3]'' & \normalsize `:di %9.3f `=R_sq1[4]'' & \normalsize `:di %9.3f `=R_sq1[5]'' & & \\ \addlinespace[0.2cm]" _newline ///
	"Change (\(j\)) vs (\(j-1\)) & - & `= incr1[2]' & `= incr1[3]' & `= incr1[4]' & `= incr1[5]' & &  \\ \addlinespace[3ex]" _newline ///
	"\cline{2-8} \addlinespace[1.5ex]" _newline ///
	"\small Basic Socio-demographics & X & X & X & X & X & & \\ \addlinespace[0cm]" _newline ///
	"\small Days on UI (last 2 years) & & X & X & X & X & & \\ \addlinespace[0cm]" _newline ///
	"\small \# Unempl. Spells (last 2 years) & & & X & X & X & & \\ \addlinespace[0cm]" _newline ///
	"\small \# Employers (last 2 years) & & & & X & X & & \\ \addlinespace[0cm]" _newline ///
	"\small Days on DI (last 2 years) & & & & & X & &  \\ \addlinespace[0cm]" _newline ///
	" \addlinespace[6ex]" _newline ///
	///
	"& \multicolumn{7}{c}{\large \textbf{D. Individual variables: marginal sub-models}} \\ \addlinespace[3ex]" _newline ///
	" & \normalsize (1) & \normalsize (2) & \normalsize (3) & \normalsize (4) & \normalsize (5) & & \\ \addlinespace[1.5ex]" _newline ///
	"\cline{2-8} \addlinespace[1.5ex]" _newline ///
	"\normalsize \(R^2(\hat{F}_{0}, F_{0})\) & \normalsize `:di %9.3f `= R_sq1[1]'' & \normalsize `:di %9.3f `=R_sq1[2]'' & \normalsize `:di %9.3f `=R_sq2[3]'' & \normalsize `:di %9.3f `=R_sq2[4]'' & \normalsize `:di %9.3f `=R_sq2[5]'' & & \\ \addlinespace[0.2cm]" _newline ///
	"Change (\(j\)) vs (1) & - & `= incr1[2]' & `= incr2[3]' & `= incr2[4]' & `= incr2[5]' & &  \\ \addlinespace[3ex]" _newline ///
	"\cline{2-8} \addlinespace[1.5ex]" _newline ///
	"\small Basic Socio-demographics & X & X & X & X & X & & \\ \addlinespace[0cm]" _newline ///
	"\small Days on UI (last 2 years) & & X & & & & &  \\ \addlinespace[0cm]" _newline ///
	"\small \# Unempl. Spells (last 2 years) & & & X & & & & \\ \addlinespace[0cm]" _newline ///
	"\small \# Employers (last 2 years) & & & & X & & & \\ \addlinespace[0cm]" _newline ///
	"\small Days on DI (last 2 years) & & & & & X & & \\ \addlinespace[0cm]" _newline ///
	" \addlinespace[3ex]" _newline ///
	///
	"\hline \hline \addlinespace[1.5ex]" _newline ///
	"\end{tabular}" _newline ///
	"%\end{table}" _newline ///
	"%\end{document}"
file close myfile	
	